<?php

/**
 * This is the model class for table "point_record".
 *
 * The followings are the available columns in table 'point_record':
 * @property integer $id
 * @property string $matric_no
 * @property string $cca_id
 * @property integer $points
 * @property string $description
 *
 * The followings are the available model relations:
 * @property CcaResident[] $ccaResidents
 */
class PointRecord extends CActiveRecord {

    public $cca_name;
    public $resident_name;
    public $role_name;
    public $resident_AY;

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Cca the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'point_record';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('matric_no, cca_id, points', 'required'),
            //		array('name, category', 'length', 'max'=>100),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('matric_no, cca_id, points, description, cca_name, resident_name, role_name, resident_AY', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'cca_points' => array(self::BELONGS_TO, 'CcaResident', 'matric_no'),
            'cca' => array(self::BELONGS_TO, 'Cca', 'cca_id'),
            'resident' => array(self::BELONGS_TO, 'Resident', 'matric_no'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
        public function attributeLabels() {
      return array(
      'id' => 'ID',
      'matric_no' => 'Matric No',
      'cca_id' => 'Cca ID',
      'points' => 'Points',
      'description' => 'Description',
      'date_recorded' => 'Date recorded',
      );
      }
     

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;
        
 

//		$criteria->compare('id',$this->id);

        $criteria->compare('matric_no', $this->matric_no);
//        $criteria->compare('cca_id', $this->cca_id);
        $criteria->compare('points', $this->points);
        $criteria->compare('description', $this->description, true);
        $criteria->compare('date_recorded', $this->date_recorded);
        $criteria->together = true;
        $criteria->with = array('cca_points', 'cca', 'resident');
        if ($this->cca_name) {
            $criteria->compare('cca.name', $this->cca_name, true);
        }
        if ($this->resident_name) {
            $criteria->compare('resident.name', $this->resident_name, true);
        }
        
        $criteria2 = new CDbCriteria;
        if (isset(Yii::app()->user->userModel)) {
            $cca_residents = Yii::app()->user->userModel->ccaResidents;
            foreach (Yii::app()->user->userModel->ccaResidents as $ccaResident) {
                if ($ccaResident->role_level < 5){
                    $criteria2->compare('cca.name', $ccaResident->cca->name, false, 'OR');
                }
            }
        }
        
        $criteria->mergeWith($criteria2, 'AND');


//        if($this->role_name)
//        {
//                $criteria->compare('role.name',$this->role_name,true);
//        }

        $sort = new CSort;
        $sort->attributes = array(
            // For each relational attribute, create a 'virtual attribute' using the public variable name
            'cca_name' => array(
                'asc' => 'cca.name',
                'desc' => 'cca.name DESC',
                'label' => 'CCA Name',
            ),  
            'resident_name' => array(
                'asc' => 'resident.name',
                'desc' => 'resident.name DESC',
                'label' => 'Name',
            ),
//		  'role_name' => array(
//			'asc' => 'role.name',
//			'desc' => 'role.name DESC',
//			'label' => 'Role',
//		  ),
            '*',
        );



        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
            'sort' => $sort,
        ));
    }

}
